package library.http;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by Mr.Fan on 2017/1/11.
 */

public class HttpManager {

    private static final Object lock = new Object();
    private static final ConcurrentHashMap<String, List<WeakReference<Cancelable>>> queue = new ConcurrentHashMap<>();

    protected static String key(Object anchor) {
        if (anchor == null)
            return null;
        return String.valueOf(anchor);
    }

    protected static void addTask(final String anchor, final Cancelable task) {
        if (anchor == null || task == null)
            return;
        synchronized (lock) {
            WeakReference<Cancelable> wrht = new WeakReference<>(task);
            if (queue.containsKey(anchor)) {
                List<WeakReference<Cancelable>> tasks = queue.get(anchor);
                tasks.add(wrht);
            } else {
                List<WeakReference<Cancelable>> tasks = Collections.synchronizedList(new ArrayList<WeakReference<Cancelable>>());
                tasks.add(wrht);
                queue.put(anchor, tasks);
            }
        }
    }

    protected static void remove(final String anchor, final Cancelable task) {
        if (anchor == null || task == null)
            return;
        synchronized (lock) {
            if (queue.containsKey(anchor)) {
                List<WeakReference<Cancelable>> tasks = queue.get(anchor);
                if (tasks != null && tasks.size() > 0) {
                    for (int i = 0; i < tasks.size(); i++) {
                        WeakReference<Cancelable> reference = tasks.get(i);
                        Cancelable check = reference.get();
                        if (check == null) {
                            tasks.remove(i);
                            continue;
                        } else if (task.equals(check)) {
//                            task.cancel();
                            tasks.remove(i);
                            return;
                        }
                    }
                }
            }
        }
    }

    protected static void cancel(final Object anchor) {
        cancel(key(anchor));
    }

    protected static void cancel(final String anchor) {
        if (anchor == null)
            return;
        synchronized (lock) {
            if (queue.containsKey(anchor)) {
                List<WeakReference<Cancelable>> tasks = queue.get(anchor);
                if (tasks != null && tasks.size() > 0) {
                    for (WeakReference<Cancelable> reference : tasks) {
                        Cancelable task = reference.get();
                        if (task != null) {
                            task.cancel();
                        }
                    }
                }
                tasks.clear();
                tasks = null;
                //移除对应的Key
                queue.remove(anchor);
            }
        }
    }

    protected static void cancelAll() {
        synchronized (lock) {
            for (Map.Entry<String, List<WeakReference<Cancelable>>> entry : queue.entrySet()) {
                List<WeakReference<Cancelable>> tasks = entry.getValue();
                if (tasks != null && tasks.size() > 0) {
                    for (WeakReference<Cancelable> reference : tasks) {
                        Cancelable task = reference.get();
                        if (task != null) {
                            task.cancel();
                        }
                    }
                }
                tasks.clear();
            }

            queue.clear();
        }
    }

}
